# Replication Archive for 
# Alexander Coppock, Emily Ekins and David Kirby (2018), 
# "The Long-lasting Effects of Newspaper Op-Eds on Public Opinion",
# Quarterly Journal of Political Science: Vol. 13: No. 1, pp 59-87.

rm(list = ls())

# Uncomment to install
# install.packages(c("xtable"))

library(xtable)

# Set working directory to replication archive

load("mturk_opeds_cleaned.rdata")
load("elite_opeds_cleaned.rdata")
load("dv_df.rdata")
source("opeds_source.R")


# MTurk Table -------------------------------------------------------------

panel_1 <- with(mturk_opeds, table(Z))
panel_2 <- with(subset(mturk_opeds, responded_w2), table(Z_distract, Z))
panel_3 <- with(mturk_opeds, table(responded_w2, Z))
panel_4 <- with(mturk_opeds, table(responded_w3, Z))

chi_panel_3 <- chisq.test(panel_3)
chi_panel_3$p.value
chi_panel_4 <- chisq.test(panel_4)
chi_panel_4$p.value

panel_1 <- t(as.matrix(c(panel_1, total=sum(panel_1))))
panel_2 <- cbind(panel_2, totals=rowSums(panel_2))[c(2,1),]
panel_3 <- cbind(panel_3, totals=rowSums(panel_3))[c(2,1),]
panel_4 <- cbind(panel_4, totals=rowSums(panel_4))[c(2,1),]

rownames(panel_1) <- "N"
rownames(panel_2) <- c("Distraction", "No Distraction")
rownames(panel_3) <- c("Responded", "Did not respond")
rownames(panel_4) <- c("Responded", "Did not respond")

print(xtable(panel_1), hline.after = c(), include.colnames = FALSE, only.contents = TRUE)
print(xtable(panel_2, digits = 0), hline.after = c(), include.colnames = FALSE, only.contents = TRUE)
print(xtable(panel_3, digits = 0), hline.after = c(), include.colnames = FALSE, only.contents = TRUE)
print(xtable(panel_4, digits = 0), hline.after = c(), include.colnames = FALSE, only.contents = TRUE)


# Elite Table -------------------------------------------------------------

panel_1 <- with(elite_opeds, table(Z))
panel_2 <- with(subset(elite_opeds, responded_w2), table(Z_distract, Z))
panel_3 <- with(elite_opeds, table(responded_w2, Z))

chi_panel_3 <- chisq.test(panel_3)
chi_panel_3$p.value

panel_1 <- t(as.matrix(c(panel_1, total=sum(panel_1))))
panel_2 <- cbind(panel_2, totals=rowSums(panel_2))[c(2,1),]
panel_3 <- cbind(panel_3, totals=rowSums(panel_3))[c(2,1),]

rownames(panel_1) <- "N"
rownames(panel_2) <- c("Distraction", "No Distraction")
rownames(panel_3) <- c("Responded", "Did not respond")

print(xtable(panel_1), hline.after = c(), include.colnames = FALSE, only.contents = TRUE)
print(xtable(panel_2, digits = 0), hline.after = c(), include.colnames = FALSE, only.contents = TRUE)
print(xtable(panel_3, digits = 0), hline.after = c(), include.colnames = FALSE, only.contents = TRUE)


